import request from '@/utils/request'
import type {
  spuData,
  spuResponseData,
  AllTrademarkList,
  SpuImgList,
  saleAttrResponseData,
  existedSaleAttrResponse,
  SkuData,
  SkuInfoData,
} from '@/api/product/type'
enum API {
  //获取已有的Spu数据
  HASSPU_URL = '/admin/product/',
  //获取所有品牌的数据
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  //获取某个SPU的售卖商品的图片
  IMAGES_URL = '/admin/product/spuImageList/',
  //获取某SPU的所有销售属性
  SPUSALEATTRLIST_URL = '/admin/product/spuSaleAttrList/',
  //获取某SPU所有的销售属性以便筛选出剩下可选的销售属性,
  ALLSPUSALE_URL = '/admin/product/baseSaleAttrList',
  //新增一个SPU
  ADDSPU_URL = '/admin/product/saveSpuInfo',
  //修改一个SPU
  UPDATESPU_URL = '/admin/product/updateSpuInfo',
  //追加一个新增的SKU地址
  ADDSKU_URL = '/admin/product/saveSkuInfo',
  //查看某一个已有的SPU下全部售卖的商品
  SKUINFO_URL = '/admin/product/findBySpuId/',
  //删除已有的SPU
  REMOVESPU_URL = '/admin/product/deleteSpu/',
}
//获取已有的某一个SPU数据
export const reqHasSpu = (page: number, limit: number, C3Id: number | string) =>
  request.get<any, spuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${C3Id}`,
  )
//获取所有品牌的相关数据
export const reqAllTrademark = () =>
  request.get<any, AllTrademarkList>(API.ALLTRADEMARK_URL)

//获取某一spu售卖商品的照片数据
export const reqSpuImageList = (spuId: number) =>
  request.get<any, SpuImgList>(API.IMAGES_URL + spuId)

//获取某一SPU的销售属性
export const reqSpuSaleAttrList = (spuId: number) =>
  request.get<any, saleAttrResponseData>(API.SPUSALEATTRLIST_URL + spuId)

//获取某SPU所有的销售属性以便筛选出剩下可选的销售属性
export const reqAllSpuSale = () =>
  request.get<any, existedSaleAttrResponse>(API.ALLSPUSALE_URL)

//新增或修改一个SPU
//通过有无id来区分  无id则为新增 有id则为修改
export const addOrUpdateSpu = (data: spuData) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU_URL, data)
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data)
  }
}

//添加SKU的请求方法
export const reqAddSku = (data: SkuData) =>
  request.post<any, any>(API.ADDSKU_URL, data)

//获取SKU数据
export const reqSkuList = (spuId: number | string) =>
  request.get<any, SkuInfoData>(API.SKUINFO_URL + spuId)

//删除已有的SPU
export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId)
